import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/index.vue'
import { useUserStore } from '@/stores/user'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/admin/login',
    name: 'adminLogin',
    component: () => import('@/views/admin/login/index.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/admin/register',
    name: 'adminRegister',
    component: () => import('@/views/admin/register/index.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/admin/forgot-password',
    name: 'adminForgotPassword',
    component: () => import('@/views/admin/forgot-password/index.vue'),
    meta: { title: '忘记密码' }
  },
  {
    path: '/admin/',
    component: Layout,
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'adminHome',
        component: () => import('@/views/admin/home/index.vue'),
        meta: { title: '首页', icon: 'home' }
      },
      {
        path: '/admin/news',
        name: 'adminNews',
        component: () => import('@/views/admin/news/index.vue'),
        meta: { title: '新闻资讯', icon: 'news' }
      },
      {
        path: '/admin/news/:id',
        name: 'adminNewsDetail',
        component: () => import('@/views/admin/news/detail.vue'),
        meta: { title: '新闻详情', hidden: true }
      },
      {
        path: '/admin/projects',
        name: 'adminProjects',
        component: () => import('@/views/admin/projects/index.vue'),
        meta: { title: '慈善项目', icon: 'folder' }
      },
      {
        path: '/admin/projects/:id',
        name: 'adminProjectDetail',
        component: () => import('@/views/admin/projects/detail.vue'),
        meta: { title: '项目详情', hidden: true }
      },
      {
        path: '/admin/donations',
        name: 'adminDonations',
        component: () => import('@/views/admin/donations/index.vue'),
        meta: { title: '我的捐赠', icon: 'money' }
      },
      {
        path: '/admin/volunteer',
        name: 'adminVolunteer',
        component: () => import('@/views/admin/volunteer/index.vue'),
        meta: { title: '志愿者', icon: 'user' }
      },
      {
        path: '/admin/users',
        name: 'adminUsers',
        component: () => import('@/views/admin/users/index.vue'),
        meta: { title: '用户管理', icon: 'user-filled' }
      },
      {
        path: '/admin/profile',
        name: 'adminProfile',
        component: () => import('@/views/admin/profile/index.vue'),
        meta: { title: '个人信息', hidden: true }
      }
    ]
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = `${to.meta.title} - 慈善项目管理系统`
  
  // 检查是否需要登录权限
  const userStore = useUserStore()
  const token = userStore.token
  const publicPages = ['/admin/login', '/admin/register', '/admin/forgot-password']
  const authRequired = !publicPages.includes(to.path)
  
  if (authRequired && !token) {
    // 如果需要认证且未登录，重定向到登录页面
    next({ path: '/admin/login', query: { redirect: to.fullPath } })
  } else if (token && publicPages.includes(to.path)) {
    // 如果已登录且访问登录相关页面，重定向到首页
    next('/admin/')
  } else {
    next()
  }
})

export default router